<?php

/**
 * KTDPrivateMessage form.
 *
 * @package    s-test
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class KTDPrivateMessageForm extends BaseKTDPrivateMessageForm
{
  public function configure()
  {
      unset(
              $this['from_player_id'],
              $this['show_in_the_sender_box']
              );
      $this->setWidgets(array(
                                "title" => new sfWidgetFormInputText(array("label"      => "Titre")),
                                "body"  => new sfWidgetFormKTDTextarea(array("label"=> "Contenu", "hrp_mode"=>true)),
                                "to"    => new icfbAutocompleteFormWidget(array( 'label'=> "Destinataires",
                                                                                 'url'  => sfContext::getInstance()->getController()->genUrl("player_autocomplete"),
                                                                                 'includeJQuery'  => false,
                                                                                 'includeJQueryUI'=> false,
                                                                                 'sendTitles'     => false)),
          ));
      $this->setValidators(array('title' => new sfValidatorString(array('required' => true)),
                                 'body'  => new sfValidatorString(),
                                 'to'    => new sfValidatorDoctrineMultipleChoice(array('model'=> 'sfGuardUser',
                                                                                'column'=> 'id',
                                                                                'required' => true))));
      $this->widgetSchema->setNameFormat("ktd_pm[%s]");
  }
  public function save($conn = null){
    $mp = parent::save($conn);
    $values = $this->getValues();
    foreach($values["to"] as $id_to){
          $t = new KTDPrivateMessageReceiver();
          $t->setKTDPrivateMessage($mp);
          $t->setToPlayerId($id_to);
          $t->save();
    }
    return $mp;
  }

}
